package com.xj.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xj.common.valid.ValidatableList;

import java.util.List;

public class JsonPaseUtil {
    public static String ObjToJsonStr(Object o){
        return ((JSONObject) JSONObject.toJSON(o)).toJSONString();
    }

    public static <T> T JSONStrToObj(String key,Class<T> clazz) {
        T t =null;
        try{
            t = JSONObject.parseObject(key, clazz);
        }catch(Exception e ) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> ValidatableList<T> JSONStrToList(String key, Class<T> clazz) {
        ValidatableList<T> tList =new ValidatableList<>() ;
        try{

            tList.setList(JSON.parseArray(key, clazz));;
        }catch(Exception e ) {
            e.printStackTrace();
        }
        return tList;
    }
}
